package com.supcalls.controller;
import com.supcalls.service.ZookeeperDistributedLock;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;

@RestController
public class LockTestController {

    private final ZookeeperDistributedLock lockService;

    public LockTestController(ZookeeperDistributedLock lockService) {
        this.lockService = lockService;
    }

    @GetMapping("/lock")
    public String testLock(@RequestParam String key) {
        boolean acquired = lockService.acquireLock(key, 5, TimeUnit.SECONDS);
        if (acquired) {
            try {
                // 模拟业务逻辑
                System.out.println("执行任务...");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                lockService.releaseLock(key);
                System.out.println("锁释放");
            }
            return "任务完成";
        } else {
            return "获取锁失败";
        }
    }
}

